package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.MarkOrderDeletedBean;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.req.ApplySalesRefundRequestBean;
import com.ymatou.iapi.trading.operate.parameter.req.SalesRefundInfoBean;
import com.ymatou.iapi.trading.operate.service.ApplySalesRefundRequestCall;
import com.ymatou.iapi.trading.operate.service.MarkOrderDeletedCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;
/**
*删除订单
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_MarkOrderDeleted {
	private static MarkOrderDeletedBean markorderdeletedBean;
	private static MarkOrderDeletedCall markorderdeletedCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除订单");
	}

	@Before
	public void caseUp() {
		markorderdeletedBean = new MarkOrderDeletedBean();
		markorderdeletedCall = new MarkOrderDeletedCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(markorderdeletedCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderDeleted_001() {
		Logger.start(true, "删除订单");
		try {
			//创建订单
			Logger.comment("前置数据准备开始");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			Logger.comment("前置数据准备结束");
			//参数
			markorderdeletedBean.setIsDelete(true);
			markorderdeletedBean.setOrderId(orderId);
			markorderdeletedBean.setUserId(userId);
			//发送请求
			markorderdeletedCall.setData(markorderdeletedBean);
			markorderdeletedCall.callService();

			Logger.verifyEquals("true", markorderdeletedCall.getSuccess(),
					"验证success");
			//数据库验证
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(
					true,
					(Boolean) database.selectOrderByOderId(orderId).get(0)
							.get("bDeleted"), "验证订单表是否删除状态");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderDeleted_002() {
		Logger.start(false, "订单号为空，删除订单失败");
		try {
			markorderdeletedBean.setIsDelete(true);
			markorderdeletedBean.setUserId(1113);
			//发送请求
			markorderdeletedCall.setData(markorderdeletedBean);
			markorderdeletedCall.callService();

			Logger.verifyEquals("false", markorderdeletedCall.getSuccess(),
					"验证success");

			Logger.verifyIsContains(ECode.ERROR400_MSG_9,
					markorderdeletedCall.getErrorMessage(), "验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderDeleted_003() {
		Logger.start(false, "用户Id不是当前订单的userId");
		try {
			//创建订单			
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			//参数
			markorderdeletedBean.setIsDelete(true);
			markorderdeletedBean.setOrderId(orderId);
			markorderdeletedBean.setUserId(20324128);
			//发送请求
			markorderdeletedCall.setData(markorderdeletedBean);
			markorderdeletedCall.callService();
			Logger.verifyEquals("false", markorderdeletedCall.getSuccess(),
					"验证success");
			Logger.verifyIsContains("用户20324128不是订单" + orderId + "的所有者",
					markorderdeletedCall.getErrorMessage(), "验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderDeleted_004() {
		Logger.start(true, "删除订单");
		try {
			//创建订单
			Logger.comment("前置数据准备开始");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal amount = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, amount, tradingId);
			// 卖家接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 发货
			orderCallService.dispatched(orderId, sellerId);

			ApplySalesRefundRequestBean applysalesrefundrequestBean = new ApplySalesRefundRequestBean();
			applysalesrefundrequestBean.setOrderId(orderId);
			// 订单商品明细
			List<Map> ymtOrdersInfoList = new DefaultWapper()
					.selectOrderInfoByOderId(orderId);
			List<SalesRefundInfoBean> salesRefundInfoList = new ArrayList<SalesRefundInfoBean>();
			BigDecimal totalPrice = new BigDecimal(0);
			for (int i = 0; i < ymtOrdersInfoList.size(); i++) {
				String catalogId = (String) ymtOrdersInfoList.get(i).get(
						"sCatalogId");
				String productId = (String) ymtOrdersInfoList.get(i).get(
						"sProductId");
				// 如果規格為null,給個默認值
				int productNum = (Integer) ymtOrdersInfoList.get(i).get(
						"iAmount");
				// 计算订单总价格 商品价格 X 数量+运费
				BigDecimal refoundAmout = ((BigDecimal) ymtOrdersInfoList
						.get(i).get("fProductPrice")).multiply(new BigDecimal(
						productNum));
				SalesRefundInfoBean salesRefundInfo = new SalesRefundInfoBean();
				salesRefundInfo.setCatalogId(catalogId);
				// productid 丢在resaon 里，校验数据用
				salesRefundInfo.setReason(productId);
				salesRefundInfo.setRefundAmount(BigDecimal.TEN);
				salesRefundInfo.setRefundBillNo("TH" + i + orderId);
				salesRefundInfo.setRefundProductNum(1);
				salesRefundInfo.setRefundType(1);

				salesRefundInfoList.add(salesRefundInfo);
			}
			applysalesrefundrequestBean
					.setSalesRefundInfoList(salesRefundInfoList);

			ApplySalesRefundRequestCall applysalesrefundrequestCall = new ApplySalesRefundRequestCall();
			applysalesrefundrequestCall.setData(applysalesrefundrequestBean);
			applysalesrefundrequestCall.callService();
			Logger.comment("前置数据准备结束");
			//参数
			markorderdeletedBean.setIsDelete(true);
			markorderdeletedBean.setOrderId(orderId);
			markorderdeletedBean.setUserId(userId);
			//发送请求
			markorderdeletedCall.setData(markorderdeletedBean);
			markorderdeletedCall.callService();

			Logger.verifyEquals("false", markorderdeletedCall.getSuccess(),
					"验证success");
			//数据库验证
			//			DefaultWapper database = new DefaultWapper();
			//			Logger.verifyEquals(true, (Boolean)database.selectOrderByOderId(orderId).get(0).get("bDeleted"), "验证订单表是否删除状态");
			//			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderDeleted_005() {
		Logger.start(true, "订单状态为退款退货中,删除订单失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			OrderCallService orderService = new OrderCallService();
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 买家付款
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			//接单 
			orderService.acceptOrder(orderId, sellerId);
			// 退货退款同步
			orderService.applySalesRefundRequestInJava(orderId);
			Logger.comment("前置数据准备结束");
			//参数
			markorderdeletedBean.setIsDelete(true);
			markorderdeletedBean.setOrderId(orderId);
			markorderdeletedBean.setUserId(userId);
			//发送请求
			markorderdeletedCall.setData(markorderdeletedBean);
			markorderdeletedCall.callService();

			Logger.verifyEquals("false", markorderdeletedCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("ERR_STATUS_NOTMATCH",
					markorderdeletedCall.getString("errorCode"), "验证errorCode");
			Logger.verifyEquals("哈尼，这个订单有一笔未完成的退货退款申请，建议完成后再删除哦~",
					markorderdeletedCall.getErrorMessage(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}